import * as Router from 'koa-router';
import { IRouterContext } from 'koa-router';
import addService from '../services/addService';
import addNewService from '../services/addNewService';
import * as fs from 'fs';
const xlsx = require('node-xlsx');
//新增日均/人均报表 
let router = new Router();
router.prefix('/api/addReportForm');
//日均拜访率
router.post('/consumerRateByDay', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    let data=await addService.visitRateByDay(res.startDate,res.endDate,res.comment,res.leaderId)
    if(data.length !=0){
        ctx.body=data;
        ctx.status=200;
    }else{
        ctx.body=[];
    } 
    await next()
});

//人均客户拜访率
router.post('/consumerRateByEmpl', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    let data=await addService.consumerRateByEmp(res.startDate,res.endDate,res.comment,res.leaderId)
    if(data.length>0){
        ctx.body=data;
        ctx.status=200;
    }else{
        ctx.body=[];
    } 
    await next()
});

//导出日均拜访率
router.post('/exportConsumerCountByDay', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    let data=await addService.exportConsumerCountByDay(res.startDate,res.endDate,res.leaderId,res.comment)
    ctx.body=data;
    ctx.status=200;
    await next()
});



//====================新增=============================
//导出人均客户拜访率=========================>接口名称已经改变了   原来的:consumerRateByEmpl>>>>>变为>>>>>exportconsumerRateByEmpl
router.post('/exportconsumerRateByEmpl', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    let data=await addService.exportConsumerCountByEmp(res.startDate,res.endDate,res.leaderId,res.comment)
    ctx.body=data;
    ctx.status=200;
    await next()
});

//导出或者显示现有客户和新客户数据
router.post('/exporNewAndNowConsumerExcel', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    console.log(res,'res_res_res_res_')
    //excel导出
    let data=await addNewService.exportNewReportForm(res.startDate,res.endDate,res.leaderId,res.character)
    let buffer = xlsx.build([{name:`新老客户excel表格`, data:data }]);
    fs.writeFileSync(`新老客户excel.xlsx`,buffer)
    console.log("----------exporNewAndNowConsumerExcel导出-------------");
    ctx.body=0;
    ctx.status=200;
    await next()
});
//显示新老客户(现有客户和新客户)拜访率
router.post('/showNewAndNowConsumerRate', async (ctx: IRouterContext, next: any) => {
    let rq: any = ctx.request
    let res = rq.body;
    //显示
    let json=await addNewService.addNewReportForm(res.startDate,res.endDate,res.leaderId,res.character,res.comment)
    ctx.body=json;
    ctx.status=200;
    await next()
});
 

export default router